package graphicinterface;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.LinkedList;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import bookworm.Review;

public class WindowRecentReviewsByTag implements MouseListener
{
	
	/**
	 * the constructor of the interface specifies a tag. With this, we search for
	 * recent reviews for this tag immediately(via facade).
	 */
	private LinkedList<Review> recentReviewsForTagSpecified;
	
	
	private JFrame windowRecentReviewsByTag;
	
	public WindowRecentReviewsByTag(String tag)
	{
		//let's ask the facade for recent reviews by this tag
		SystemFunctionalitiesFacade facade = SystemFunctionalitiesFacade.getInstance();
		this.recentReviewsForTagSpecified = facade.getRecentReviewsByTag(tag);
		windowRecentReviewsByTag = new JFrameWithMenu("Recent reviews by tag " + tag);
		this.drawWindowRecentReviewsByTag();
	}
	
	private void drawWindowRecentReviewsByTag()
	{
		//first, let's create the components for the screen
		//a label with a mascot character...
		ImageIcon imageHelperCharacter = CreatesImageIcon.createImageIcon("images/meganechan.png", "mascot character");
		//how do we display an ImageIcon in a panel? Through a Jlabel!
		JLabel labelMascotCharacter = new JLabel(imageHelperCharacter);
		
		//and a panel to show recent book reviews
		JPanel panelShowsRecentReviews = this.createPanelShowRecentReviews();
		//now, let's join these panels in one main panel
		JPanelWithBackgroundImage panelRecentReviewsByTag = new JPanelWithBackgroundImage("backgroundbookworm.jpg");
		GridBagLayout layoutMainPanel = new GridBagLayout();
		panelRecentReviewsByTag.setLayout(layoutMainPanel);
		GridBagConstraints g = new GridBagConstraints();
		g.gridx = 0;
		g.gridwidth = 2;
		panelRecentReviewsByTag.add(labelMascotCharacter, g);
		g.gridx = 3;
		g.gridwidth = 6;
		panelRecentReviewsByTag.add(panelShowsRecentReviews, g);
		
		this.windowRecentReviewsByTag.add(panelRecentReviewsByTag);
		this.windowRecentReviewsByTag.setVisible(true);
	}
	
	/**
	 * this method creates the main panel of the window: the panel showing reviews in a list
	 * with reviews' creation date, review title and a mascot character. The user can
	 * click on a single review to view the review's details.
	 * @return
	 */
	private JPanel createPanelShowRecentReviews()
	{
		//each review from list recentReviewsForTagSpecified will have a panel associated to it.
		LinkedList<JPanel> panelsRecentReviewsForTag = new LinkedList<JPanel>();
		for(int i = 0; i < this.recentReviewsForTagSpecified.size(); i++)
		{
			Review singleReview = this.recentReviewsForTagSpecified.get(i);
			/*first, let's create a panel with a mascot character and review title. It's the PanelSingleReview. 
			 */
			PanelSingleReview panelMascotAndReviewTitle = new PanelSingleReview(singleReview);
			//each of these panels is selectable, for the user to see a single review
			panelMascotAndReviewTitle.addMouseListener(this);
			//aren't we missing something!ah,yes!the review's date of creation and book title...
			String reviewDateCreated = singleReview.getReviewDate();
			JLabel labelReviewDateCreation = TextLabelCreator.createStyledTextlabel(reviewDateCreated, 15);
			String bookTitle = singleReview.getBookTitle();
			if(bookTitle.length() > 40)
			{
				//book title is too big, let's reduce it
				bookTitle = bookTitle.substring(0, 40) + "...";
			}
			JLabel labelBookTitle = TextLabelCreator.createStyledTextlabel("from book:" + bookTitle, 15, Color.BLUE);
			//now let's join these single review informations in a single panel!
			JPanel panelRecentSingleReview = new JPanel();
			panelRecentSingleReview.setBackground(new Color(255,131,137));
			GridBagLayout layoutPanelRecentReview = new GridBagLayout();
			panelRecentSingleReview.setLayout(layoutPanelRecentReview);
			GridBagConstraints g = new GridBagConstraints();
			g.gridy = 0;
			g.gridheight = 1;
			panelRecentSingleReview.add(labelReviewDateCreation, g);
			g.gridy = 1;
			g.gridheight = 1;
			panelRecentSingleReview.add(labelBookTitle, g);
			g.gridy = 2;
			g.gridheight = 3;
			panelRecentSingleReview.add(panelMascotAndReviewTitle, g);
			
			panelsRecentReviewsForTag.add(panelRecentSingleReview);
		}
		
		/*now that we created the panels for each review, let's join them in a panel.
		 *  But we don't want all the recent reviews to be shown in the screen at the same time : they would occupy a lot of space!
		 *  We'll only show 2 per page with the Jpanel we created: ShowsJPanelsOnManyPages! 
		 */
		ShowsJPanelsOnManyPages showsRecentReviews = new ShowsJPanelsOnManyPages(panelsRecentReviewsForTag, 2, 2);
		
		return showsRecentReviews;
	}
	
	

	@Override
	public void mouseClicked(MouseEvent arg0) 
	{
		/*
		 * the panels representing single reviews already show a single review when clicked, so
		 * all we need to do in this window is dispose the main frame
		 */
		this.windowRecentReviewsByTag.dispose();
		
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	
	
	

}
